package com.difsonapps.android.marker.dialogs;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.difsonapps.android.marker.AppConstant;
import com.difsonapps.android.marker.CustomRequest;
import com.difsonapps.android.marker.IRefresher;
import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.app.AppController;
import com.difsonapps.android.marker.beans.Album;
import com.difsonapps.android.marker.beans.Photo;
import com.difsonapps.android.marker.beans.PhotoUpload;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class EditAlbumDialog extends Dialog{
	Activity activity;
	Album album;
	IRefresher<Album> refresher;
	
	
	EditText nameEditText;
	EditText descriptionEditText;
	
	public EditAlbumDialog(Activity activity, Album album, IRefresher<Album> refresher) {
		super(activity);
		this.activity = activity;
		setTitle(R.string.editing_album);
		this.album = album;
		this.refresher = refresher;
		
		setContentView(R.layout.dialog_edit_album);
		nameEditText = (EditText)findViewById(R.id.name_text);
		descriptionEditText = (EditText)findViewById(R.id.description_text);
		findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				save();
			}
		});
		findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancelAction();
			}
		});
		
		if(album!=null){
			nameEditText.setText(album.getName());
			descriptionEditText.setText(album.getDesc());
		}
	}
	
	private void save(){
		updateAlbum();
	}
	
	private void cancelAction(){
		dismiss();
	}
	
	ProgressDialog loadingProgressDialog;
	
	private void updateAlbum(){
		if(album==null) return;
		Map<String, String> params = new HashMap<String, String>();
		params.put("id", ""+album.getId());
		params.put("name", nameEditText.getText().toString());
		params.put("description", descriptionEditText.getText().toString());
		CustomRequest jsObjRequest = new CustomRequest(
				Method.POST, 
				AppConstant.editalbumUrl, 
				params,
				new Response.Listener<JSONObject>() {
					 
                    @Override
                    public void onResponse(JSONObject response) {
                    	//hideProgress();
                    	toast("Response : "+response.toString());
                    	Log.i("Test", response.toString());
                    	parseJsonUpdateAlbum(response);
                    }
                },
                new Response.ErrorListener() {
 
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    	if(loadingProgressDialog!=null) loadingProgressDialog.dismiss();
                    	//hideProgress();
                    	toast("Error: " + error.getMessage());
                    	Log.e("Test", error.getMessage());
                    }
                }
        );

       AppController.getInstance().addToRequestQueue(jsObjRequest);
       loadingProgressDialog = ProgressDialog.show(activity, activity.getResources().getString(R.string.loading), activity.getResources().getString(R.string.saving), true);
       loadingProgressDialog.setCancelable(true);
	}
	/**
	 * Parse the result of upload of an photo
	 * If it's the first photo, get the album
	 * Upload the next photo if
	 */
	private void parseJsonUpdateAlbum(JSONObject response){
		if(loadingProgressDialog!=null) loadingProgressDialog.dismiss();
		try {
        	//toast(response.toString());
            if(response.has("success")&&response.has("album")){
            	String success = response.getString("success");
            	toast(success);
            	Album album = Album.parseFromJsonObject(response.getJSONObject("album"));
            	if(album!=null) {
            		if(refresher!=null) refresher.refresh(album);
            		dismiss();
            	}
            	return;
            }else if(response.has("error")){
            	String error = response.getString("error");
            	toast(error);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
	}

	private void toast(String text){
		Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
	}

}
